'use strict';
/**
 * 莫凡淘宝 2.0 后端 controller
 */

const schemaJoi = require('./utility/schemaJoi.js');
const Joi = require('joi');
const basiCode = require('./utility/basiCode.js');
const indexService = require('./service/indexService.js');

exports.main = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			id: Joi.number().required(),
			name: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.main(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};


exports.getMember = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			nickName: Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getMember(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

exports.getMemberUrl = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			url: Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getMemberUrl(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};


exports.getiIntegral = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			nickName: Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getiIntegral(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

exports.changeIntegral = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			nickName: Joi.string().required(),
      num: Joi.number().required(),
      mixNick: Joi.string().required(),
      opt_type:  Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.changeIntegral(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

exports.getCategoryList = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
      parent_cid: Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);
		//业务处理
		const retObj = await indexService.getCategoryList(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

exports.getGoodsList = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			index: Joi.string().required(),
      page_size: Joi.string().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getGoodsList(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

exports.getGoodsItem = async (context) => {
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			num_iids: Joi.array().required(),
			session: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getGoodsItem(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};


// 获取session
exports.getSession = async (context) => {
  // return context.data
	let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			code: Joi.string().required(),
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
    
		const retObj = await indexService.getSession(ctx);
		return await basiCode(0, retObj);
	} catch (e) {
		return await basiCode(e.code || 8000, e);
	}
};

